package com.ddyx.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.http.client.ClientProtocolException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ddyx.entity.Goods;
import com.ddyx.entity.GoodsImage;
import com.ddyx.mapper.GoodsImageMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.service.IToolService;
import com.ddyx.util.QRCode_wxProgram;
import com.ddyx.util.QiniuUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.util.Utils;
import com.ddyx.util.WxCommonUtil;
import com.ddyx.util.template.TemplateMessageUtil;

@Service
public class ToolServiceImpl implements IToolService {

	@Resource
	GoodsMapper goodsMapper;

	@Resource
	GoodsImageMapper goodsImageMapper;
	
	@SuppressWarnings({ "unchecked", "static-access" })
	@Override
	public ResponseForm getQRCode_wxProgram(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			Map<String, Object> map = new HashMap<>();
			String page=paramMap.get("page")+"";
			String scene=paramMap.get("scene")+"";
			String width="10000";
			String imgName=new Utils().getUUID();
			
			
			//String page="pages/home/home";//你二维码中跳向的地址
			//String scene="B02S0002,B0000001,12";
		    //map.put("page", "pages/product/product");
		    //map.put("width", "230");//图片大小
		    //scene 最多支持 20个 参数
		    //map.put("scene", "B02S0002,B0000001,12");
			try {
				//获取二维码图片
				String localFilePath=new QRCode_wxProgram().GetUrl(page, scene, width, imgName);
				if(!StringUtils.hasText(localFilePath)) {
					result.setStatus(false);
					result.setMessage("操作有误");
					return result;
				}
				//上传七牛
				String qiniuUrl=new QiniuUtil().getQiNiuUrl(localFilePath);
				if(!StringUtils.hasText(qiniuUrl)) {
					result.setStatus(false);
					result.setMessage("操作有误");
					return result;
				}
				map.put("imgUrl", qiniuUrl);
				result.setData(map);
			} catch (ClientProtocolException e) {
				result.setStatus(false);
				result.setMessage("操作有误");
				e.printStackTrace();
			} catch (IOException e) {
				result.setStatus(false);
				result.setMessage("操作有误");
				e.printStackTrace();
			}
			return result;
		}
		return null;
	}

	@Override
	public ResponseForm getAddGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			try {
				List<Map<String, String>> li=new ArrayList<>();
				Map<String, String> map1=new HashMap<>();
				map1.put("storeId", "4");
				map1.put("storeNo", "B02S0005");
				map1.put("aa", "2");
				Map<String, String> map2=new HashMap<>();
				map2.put("storeId", "5");
				map2.put("storeNo", "B02S0006");
				map2.put("aa", "1");
				Map<String, String> map3=new HashMap<>();
				map3.put("storeId", "6");
				map3.put("storeNo", "B02S0007");
				map3.put("aa", "2");
				Map<String, String> map4=new HashMap<>();
				map4.put("storeId", "7");
				map4.put("storeNo", "B02S0008");
				map4.put("aa", "1");
				Map<String, String> map5=new HashMap<>();
				map5.put("storeId", "8");
				map5.put("storeNo", "B02S0009");
				map5.put("aa", "2");
				li.add(map1);
				li.add(map2);
				li.add(map3);
				li.add(map4);
				li.add(map5);
				Goods wanbogoods=new Goods();
				wanbogoods.setStoreId(1);
				wanbogoods.setDiscountStatus(0);
				List<Goods> wanbogoodslist=goodsMapper.select(wanbogoods);
				Goods oshiligoods=new Goods();
				oshiligoods.setStoreId(2);
				oshiligoods.setDiscountStatus(0);
				List<Goods> oshiligoodslist=goodsMapper.select(oshiligoods);
				Integer goodsId=396;
				for (int i = 0; i < li.size(); i++) {
					Map<String, String> map=li.get(i);
					if("1".equals(map.get("aa"))) {
						for (int j = 0; j < wanbogoodslist.size(); j++) {
							goodsId++;
							Goods bean=wanbogoodslist.get(j);
							Integer oldgoodsId=bean.getGoodsId();
							bean.setStoreId(Integer.parseInt(map.get("storeId")));
							bean.setStoreNo(map.get("storeNo"));
							bean.setGoodsId(goodsId);
							goodsMapper.insertSelective(bean);
							
							GoodsImage goodsImage=new GoodsImage();
							goodsImage.setGoodsId(oldgoodsId);
							List<GoodsImage> imagelist=goodsImageMapper.select(goodsImage);
							for (int k = 0; k < imagelist.size(); k++) {
								GoodsImage goodsImage2=imagelist.get(k);
								GoodsImage goodsImage3=new GoodsImage();
								goodsImage3.setGoodsId(goodsId);
								goodsImage3.setImageUrl(goodsImage2.getImageUrl());
								goodsImageMapper.insertSelective(goodsImage3);
							}
						}
					}else {
						for (int j = 0; j < oshiligoodslist.size(); j++) {
							goodsId++;
							Goods bean=oshiligoodslist.get(j);
							Integer oldgoodsId=bean.getGoodsId();
							bean.setStoreId(Integer.parseInt(map.get("storeId")));
							bean.setStoreNo(map.get("storeNo"));
							bean.setGoodsId(goodsId);
							goodsMapper.insertSelective(bean);
							
							GoodsImage goodsImage=new GoodsImage();
							goodsImage.setGoodsId(oldgoodsId);
							List<GoodsImage> imagelist=goodsImageMapper.select(goodsImage);
							for (int k = 0; k < imagelist.size(); k++) {
								GoodsImage goodsImage2=imagelist.get(k);
								GoodsImage goodsImage3=new GoodsImage();
								goodsImage3.setGoodsId(goodsId);
								goodsImage3.setImageUrl(goodsImage2.getImageUrl());
								goodsImageMapper.insertSelective(goodsImage3);
							}
						}
					}
				}
				result.setStatus(false);
				result.setMessage("操作有误");
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
			}
			return result;
		}
		return null;
	}
	
	public static void main(String[] args) {
		int in=1;
		for (int i = 0; i < 20; i++) {
			in++;
			System.out.println(in);
		}
		
	}

	@Override
	public ResponseForm getWxToken() {
		ResponseForm result = new ResponseForm();
		String token = TemplateMessageUtil.getToken();
		if(!StringUtils.hasText(token)||token == ""||token == null) {
			result.setStatus(false);
			result.setMessage("get token failure !");
		}
		result.setData(token);
		return result;
	}

	@Override
	public ResponseForm wxPicUpload(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map paramMap = (Map) param.getData();
		String goodsPic = (String) paramMap.get("goodsPic");
		String jsonResult = WxCommonUtil.wxPicUpload(goodsPic);
		result.setData(jsonResult);
		return result;
	}
	
}
